package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Banner;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.query.BannerQuery;
import cn.wolfcode.wolf2w.service.IBannerService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 游记推荐控制器
 */
@Controller
@RequestMapping("banner")
public class BannerController {

    @Autowired
    private IBannerService bannerService;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private ITravelService travelService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")BannerQuery qo){
        Page<Banner> page = bannerService.query(qo);
        model.addAttribute("page",  page);
        return "banner/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Banner banner){
        if (StringUtils.hasLength(banner.getId())) {
            bannerService.updateById(banner);
        } else {
            bannerService.insert(banner);
        }
        return JsonResult.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        if (StringUtils.hasLength(id)) {
            bannerService.deleteById(id);
        }
        return JsonResult.success();
    }

    //  添加编辑，游记/攻略显示
    @RequestMapping("/getAllType")
    @ResponseBody
    public Object getAllType(){

        List<Travel> travelList = travelService.selectAll();
        List<Strategy> strategyList = strategyService.selectAll();

        Map<String, Object> map = new HashMap<>();

        map.put("ts", travelList);
        map.put("sts", strategyList);

        return JsonResult.success(map);
    }


}
